<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/6/17
 * Time: 下午7:58
 */

namespace app\mall\controller;


use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\QrCode;
use extend\Functions;
use extend\STATUS_CODE;
use think\facade\App;
use think\Request;

class Member extends Auth
{
    /**
     * @purpose 获取代理商升级条件
     * @return array
     */
    public function agentUpgradeConditions ()
    {
        return \app\mall\logic\Member::agentUpgradeConditions(self::$user_info['invitation']);
    }
    /**
     * @purpose 获取邀请二维码
     * @return array
     * @throws \Endroid\QrCode\Exception\InvalidPathException
     */
    public function QrCode (){
        $path       = App::getRootPath().'uploads/avatar/';
        $avatar     = $path.self::$user_info['uuid'].'.png';

        $invitation_path = App::getRootPath().'uploads/invitation/';
        $invitation_qr   = $invitation_path.self::$user_info['uuid'].'.png';
        $data = '';
        if (is_file($invitation_qr)) {
            $data = file_get_contents($invitation_qr);
        } else {
            if (!is_file($avatar)) {
                if (!is_dir($path)) {
                    mkdir($path, 0777, true);
                }
                $avatar_res = file_get_contents(self::$user_info['avatar']);
                file_put_contents($avatar, $avatar_res);
            }
            $qr_code = new QrCode(config('client_domain').'?invitation='.self::$user_info['invitation']);
            $qr_code->setWriterByName('png');
            $qr_code->setEncoding('UTF-8');
            $qr_code->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
            $qr_code->setLogoPath($avatar);
            $qr_code->setLogoWidth(80);
            $data = $qr_code->writeString();
            if (!is_dir($invitation_path)) {
                mkdir($invitation_path, 0777, true);
            }
            $qr_code->writeFile($invitation_qr);
        }
        $data = 'data:image/png;base64,' . base64_encode($data);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            [
                'img' => $data
            ]
        );
    }
    /**
     * @purpose 获取用户信息
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info ()
    {
        return \app\mall\logic\Member::infoByToken(self::$token);
    }

    /**
     * @purpose 获取一级盟友
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function invitationFirst(Request $request)
    {
        $page       = $request->get('page', 1);
        $size       = $request->get('size', 100);
        return      \app\mall\logic\Member::invitationFirst(self::$user_info['invitation'], $page, $size);
    }

    /**
     * @purpose 获取二级盟友
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function invitationSecond(Request $request)
    {
        $page       = $request->get('page', 1);
        $size       = $request->get('size', 100);
        return      \app\mall\logic\Member::invitationSecond(self::$user_info['invitation'], $page, $size);
    }

    /**
     * @purpose 获取一二级盟友的数量
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function invitationTotal()
    {
        return      \app\mall\logic\Member::invitationTotal(self::$user_info['invitation']);
    }
}